package com.yuexun.dhdoctor.module.meetingNew.ui;

import android.content.Intent;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.baselistener.ISearchCallBack;
import com.yuexun.dhdoctor.module.meetingNew.ui.fragment.Frag_MeetingNew_AbsenceDuty;
import com.yuexun.dhdoctor.module.meetingNew.ui.fragment.Frag_MeetingNew_Get;
import com.yuexun.dhdoctor.module.meetingNew.ui.fragment.Frag_MeetingNew_StartMy;
import com.yuexun.dhdoctor.module.meetingNew.ui.fragment.Frag_MeetingNew_WithMe;
import com.yuexun.dhdoctor.util.DateUtil;

import org.kymjs.kjframe.ui.KJFragment;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by work-jx on 2016/11/15.
 */
public class Act_MeetingNew_Main extends BaseKJActivity {

    @Bind(R.id.tv_week_title)
    TextView tvWeekTitle;
    @Bind(R.id.tv_week_date)
    TextView tvWeekDate;

    @Bind(R.id.tv_get)
    TextView tvGet;
    @Bind(R.id.img_get)
    ImageView imgGet;
    @Bind(R.id.tv_with_me)
    TextView tvWithMe;
    @Bind(R.id.img_with_me)
    ImageView imgWithMe;
    @Bind(R.id.tv_start_my)
    TextView tvStartMy;
    @Bind(R.id.img_start_my)
    ImageView imgStartMy;
    @Bind(R.id.unread_tv)
    TextView unreadTv;
    @Bind(R.id.tv_absence_duty)
    TextView tvAbsenceDuty;
    @Bind(R.id.img_absence_duty)
    ImageView imgAbsenceDuty;
    @Bind(R.id.rl_absence_duty)
    RelativeLayout rlAbsenceDuty;


    public Date nowMeetingWeek;
    //页面标记
    private int pageType = 1;

    KJFragment mContentFragment;
    Frag_MeetingNew_Get fragment_get;
    Frag_MeetingNew_WithMe fragment_withme;
    Frag_MeetingNew_StartMy fragment_my;
    Frag_MeetingNew_AbsenceDuty fragment_duty;

    private ISearchCallBack mCallBack;

    public void setOnSearchCallBack(ISearchCallBack callBack) {
        this.mCallBack = callBack;
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.act_meetingnew_main);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        nowMeetingWeek = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE).getTime();

        tvWeekTitle.setText(DateUtil.getWeekOfMonth(nowMeetingWeek));
        String weekDateString = String.format("%s-%s",
                DateFormat.format("MM.dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)),
                DateFormat.format("MM.dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)));
        tvWeekDate.setText(weekDateString);

        fragment_get = new Frag_MeetingNew_Get();
        fragment_withme = new Frag_MeetingNew_WithMe();
        fragment_my = new Frag_MeetingNew_StartMy();
        fragment_duty = new Frag_MeetingNew_AbsenceDuty();

        mContentFragment = fragment_get;
        setOnSearchCallBack((ISearchCallBack) mContentFragment);
        changeFragment(R.id.things_content, mContentFragment);
        rlAbsenceDuty.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.back, R.id.things_add, R.id.iv_preview,
            R.id.iv_next, R.id.ll_get, R.id.rl_with_me, R.id.rl_start_my, R.id.unread_tv, R.id.rl_absence_duty})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.things_add:
                Intent intent = new Intent(this, Act_MeetingNew_Add.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.iv_preview:
                previewWeekMeeting();
                break;
            case R.id.iv_next:
                nextWeekMeeting();
                break;
            case R.id.unread_tv:
                intent = new Intent(this, Act_MeetingNew_UnRead.class);
                startActivity(intent);
                break;
            case R.id.ll_get:
                tvGet.setTextColor(getResources().getColor(R.color.doc_advice_01));
                imgGet.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                tvWithMe.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgWithMe.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvStartMy.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgStartMy.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvAbsenceDuty.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgAbsenceDuty.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                mContentFragment = fragment_get;
                setOnSearchCallBack((ISearchCallBack) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                if (pageType != 1) {
                    pageType = 1;
                }
                break;
            case R.id.rl_with_me:
                tvGet.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgGet.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvWithMe.setTextColor(getResources().getColor(R.color.doc_advice_01));
                imgWithMe.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                tvStartMy.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgStartMy.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvAbsenceDuty.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgAbsenceDuty.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                mContentFragment = fragment_withme;
                setOnSearchCallBack((ISearchCallBack) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                if (pageType != 2) {
                    pageType = 2;
                }
                break;
            case R.id.rl_start_my:
                tvGet.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgGet.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvWithMe.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgWithMe.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvStartMy.setTextColor(getResources().getColor(R.color.doc_advice_01));
                imgStartMy.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                tvAbsenceDuty.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgAbsenceDuty.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                mContentFragment = fragment_my;
                setOnSearchCallBack((ISearchCallBack) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                if (pageType != 3) {
                    pageType = 3;
                }
                break;
            case R.id.rl_absence_duty:
                tvGet.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgGet.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvWithMe.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgWithMe.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvStartMy.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgStartMy.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvAbsenceDuty.setTextColor(getResources().getColor(R.color.doc_advice_01));
                imgAbsenceDuty.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                mContentFragment = fragment_duty;
                setOnSearchCallBack((ISearchCallBack) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                if (pageType != 4) {
                    pageType = 4;
                }
                break;
        }
    }

    //上一周
    private void previewWeekMeeting() {
        Calendar calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        calendar.setTime(nowMeetingWeek);
        calendar.add(Calendar.DATE, -7);

        nowMeetingWeek = calendar.getTime();

        tvWeekTitle.setText(DateUtil.getWeekOfMonth(calendar.getTime()));
        String weekDateString = String.format("%s-%s",
                DateFormat.format("MM.dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)),
                DateFormat.format("MM.dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)));
        tvWeekDate.setText(weekDateString);

        String startDate = DateFormat.format("yyyy-MM-dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)).toString();
        String endDate = DateFormat.format("yyyy-MM-dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)).toString();

        mCallBack.searchBack(startDate + "/" + endDate);
    }

    //下一周
    private void nextWeekMeeting() {
        Calendar calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        calendar.setTime(nowMeetingWeek);
        calendar.add(Calendar.DATE, 7);

        nowMeetingWeek = calendar.getTime();

        tvWeekTitle.setText(DateUtil.getWeekOfMonth(calendar.getTime()));
        String weekDateString = String.format("%s-%s",
                DateFormat.format("MM.dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)),
                DateFormat.format("MM.dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)));
        tvWeekDate.setText(weekDateString);


        String startDate = DateFormat.format("yyyy-MM-dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)).toString();
        String endDate = DateFormat.format("yyyy-MM-dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)).toString();

        mCallBack.searchBack(startDate + "/" + endDate);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 100:
                pageType = 3;

                tvGet.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgGet.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvWithMe.setTextColor(getResources().getColor(R.color.doc_advice_03));
                imgWithMe.setBackgroundColor(getResources().getColor(R.color.doc_advice_05));
                tvStartMy.setTextColor(getResources().getColor(R.color.doc_advice_01));
                imgStartMy.setBackgroundColor(getResources().getColor(R.color.doc_advice_01));
                mContentFragment = fragment_my;
                setOnSearchCallBack((ISearchCallBack) mContentFragment);
                changeFragment(R.id.things_content, mContentFragment);
                break;
        }
    }

}
